package com.manager.enterprise.module.service;

import com.manager.commons.mybatis.dao.GenericDao;
import com.manager.commons.mybatis.service.GenericService;
import com.manager.commons.utils.PinyinUtil;
import com.manager.commons.utils.StringUtil;
import com.manager.commons.utils.UUIDUtil;
import com.manager.core.web.json.AjaxJson;
import com.manager.enterprise.module.dao.GenFileUserDao;
import com.manager.enterprise.module.dao.GenFilesDao;
import com.manager.enterprise.module.dao.GenUserDao;
import com.manager.enterprise.module.entity.GenFileUser;
import com.manager.enterprise.module.entity.GenUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Map;


/**
 * GenUserService
 * 用户表
 *
 * @author manager
 * @version 1.0
 */
@Service
public class GenUserService extends GenericService<GenUser> {

    @Autowired
    private GenUserDao jdbcDao;
    @Autowired
    private GenFileUserDao fileUserDao;

    @Override
    public GenericDao<GenUser> getDao() {
        return jdbcDao;
    }

    /**
     * 保存
     *
     * @param params
     * @return
     */
    @Transactional(rollbackFor = {RuntimeException.class, Exception.class})
    public AjaxJson mySave(Map<String, Object> params) throws Exception {
        if (null != params) {
            String _id = String.valueOf(params.get("_id"));
            String name = String.valueOf(params.get("name"));
            String py = PinyinUtil.toPinyin(name);
            if (!params.containsKey("editOpen")) {
                params.put("editOpen", 0);
            }
            int f = 0;
            params.put("pinyin", py);
            GenFileUser genFileUser = new GenFileUser();
            genFileUser.setCreator(getCreator(params));
            genFileUser.setGfId(getParamsString(params, "goId"));
            if (StringUtil.isEmpty(_id)) {
                Map<String, Object> numberInfo = jdbcDao.queryUserNumber(params);
                int userTotal = getParamsInt(numberInfo, "userTotal");
                int userNumber = getParamsInt(numberInfo, "userNumber");
                if (userNumber > 0 && userTotal >= userNumber) {
                    return new AjaxJson(0, "用户创建人数已达上限");
                }
                String pid = jdbcDao.phoneCount(params);
                //如果查询到电话号码存在，且不是当前用户的，则提示电话号码已被使用
                if (StringUtil.isNotEmpty(pid) && !_id.equals(pid)) {
                    return new AjaxJson(0, "电话号码已被注册，请重新输入");
                }
                String uuid = UUIDUtil.getInstance().getUUID();
                params.put("_id", uuid);
                params.put("id", uuid);
                params.put("password", "DD6E5E5918E94D997C686FCEBC56922F");
                f = insertMap(params);
                genFileUser.setGuId(uuid);
                genFileUser.setId(UUIDUtil.getInstance().getUUID());
                f += fileUserDao.insert(genFileUser);
            } else {
                params.put("id", _id);
                f = updateMap(params);
                genFileUser.setGuId(_id);
                f += fileUserDao.update(genFileUser);
            }
            if (f == 2) {
                return new AjaxJson(1, "保存成功");
            } else {
                throw new Exception("数据保存异常，请稍后再试！");
            }
        }
        return new AjaxJson("异常错误，请稍后再试");
    }

    /**
     * 重置
     *
     * @param params
     * @return
     */
    public AjaxJson reset(Map<String, Object> params) {
        int f = 0;
        if (params != null) {
            params.put("password", "DD6E5E5918E94D997C686FCEBC56922F");
            f = jdbcDao.resetPassword(params);
        }
        return new AjaxJson(f, f > 0 ? "密码重置成功" : "密码重置失败");
    }
}
